import storageApi from '../../../core/storage/storage';

function leaderTopController($rootScope, $scope, homeStatsService) {
  'ngInject';

  /*eslint-disable */
  this.view = {
    bidList: [],
    JLList: [],
  };


  // 点击标段
  this.onClickBidItem = (value) => {
    this.view.bidList.forEach((item) => {
      item.active = false;
    });
    this.view.JLList.forEach((item) => {
      item.active = false;
    });
    value.active = true;
    this.ngModel = value;
  };

  // 点击监理
  this.onClickJLItem = (value) => {
    this.view.bidList.forEach((item) => {
      item.active = false;
    });
    this.view.JLList.forEach((item) => {
      item.active = false;
    });
    value.active = true;
    this.view.bgJLIndex = value.JLIndex;
    // this.getBidListByJLId(value);

    this.ngModel = value;
  };

  this.getImgPath = (value) => {
    if (value.active) {
      return require('./../../../assets/images/leaderHome/on_click.png');
    }
    if (value.hover) {
      return require('./../../../assets/images/leaderHome/on_move.png');
    }
    if(value.JLIndex) {
      return require('./../../../assets/images/leaderHome/on_default2.png');
    }
    return require('./../../../assets/images/leaderHome/on_default1.png');
  };

  this.getBgImg = () => {
    return 'bg' + this.view.bgJLIndex;
  }

  this.getBidListByJLId = (value) => {
    this.view.bidList = [];
    this.view.bidList_copy.forEach((item) => {
      if (item.parentId == value.id) {
        this.view.bidList.push(item);
      }
    });
  }

  this.init = () => {
    this.view.JLList = [];
    this.view.bidList_copy = [];
    homeStatsService
      .queryOrgList()
      .then((res) => {
        if (res.success) {
          const orgId = $rootScope.orgId || storageApi.get('orgId');
          const orgType = $rootScope.orgType || storageApi.get('orgType');
          res.data.forEach((item) => {
            if (item.type == '2' && (item.id == orgId || orgType == 1)) {
              item.JLIndex = item.sort - 99;
              this.view.JLList.push(item);
            }
            if (item.type == '3' && (item.parentId == orgId || orgType == 1)) {
              item.index = item.sort;
              this.view.bidList_copy.push(item);
            }
          });
          this.view.JLList = _.sortBy(this.view.JLList, 'sort');
          this.view.bidList_copy =_.sortBy(this.view.bidList_copy, 'sort');
          this.view.bidList = this.view.bidList_copy;
        }
    });
  };

  this.init();
}

export default leaderTopController;
